package vivek.project.pocketmoney.common.utils
{
	import mx.collections.ArrayCollection;

	public class ArrayUtils
	{

		public static function copyAllElements(copyFrom:ArrayCollection, copyTo:ArrayCollection):void
		{
			var obj:Object=new Object();
			for each (obj in copyFrom)
			{
				copyTo.addItem(obj);
			}
		}

		public static function copyAllElementsFromArray(copyFrom:Array, copyTo:ArrayCollection):void
		{
			var obj:Object=new Object();
			for each (obj in copyFrom)
			{
				copyTo.addItem(obj);
			}
		}

		public static function findMatchingItem(object:Object, source:Array, fields:Array):Array
		{
			//TODO : to fix this for performance 
			//trace(new Date().toTimeString()+"\tArrayUtils:findMatchingItem\tstart");
			var resultArray:Array=new Array();
			var obj:Object=new Object();
			var field:Object=new Object();
			for each (obj in source)
			{
				var match:Boolean=true;
				for each (field in fields)
				{
					if (obj[field] != object[field])
						match=false;
					else
					{
						//trace (obj[field],object[field] );
					}
				}
				if (match)
				{
					resultArray.push(obj);
				}
					//	trace(new Date().toTimeString()+"\tArrayUtils:findMatchingItem\tEnd");
			}
			return resultArray;
		}

		public static function findMaximum(source:ArrayCollection, field:String):Object
		{
			var resultArray:Array=new Array();
			var obj:Object=new Object();
			var minimum:Object=new Object();
			for each (obj in source)
			{
				if (obj[field] > minimum[field] || minimum[field] == null)
					minimum=obj;
				else
				{
					//trace (obj[field],object[field] );
				}
			}
			return minimum;
		}

		public static function findMinimum(source:ArrayCollection, field:String):Object
		{
			var resultArray:Array=new Array();
			var obj:Object=new Object();
			var minimum:Object=new Object();
			minimum=obj;
			for each (obj in source)
			{
				if (obj[field] < minimum[field] || minimum[field] == null)
					minimum=obj;
			}
			return minimum;
		}

		public static function joinArray(firstArray:Array, secondArray:Array):Array
		{
			var object:Object=new Object();
			var finalArray:Array=new Array();
			for each (object in firstArray)
			{
				finalArray.push(object);
			}
			for each (object in secondArray)
			{
				finalArray.push(object);
			}
			return finalArray;
		}

		public static function joinArrayCollection(firstArray:ArrayCollection, secondArray:ArrayCollection):Array
		{
			var object:Object=new Object();
			var finalArray:Array=new Array();
			for each (object in firstArray)
			{
				finalArray.push(object);
			}
			for each (object in secondArray)
			{
				finalArray.push(object);
			}
			return finalArray;
		}

		public function ArrayUtils()
		{
		}
	}
}